package util;

import java.awt.Color;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

import model.Receiver;
import model.User;

public class SocketUtil {
	private static Socket socket;
	private static Color color;
	private static Receiver receiver;
	private static User user = new User();

	public static void StartServer() {
		ServerSocket server;
		try {
			server = new ServerSocket(6666);
			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						socket = server.accept();
						System.out.println("server link success");
						receiver.StartThread();
						color=Color.black;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();
		} catch (IOException e) {
			;
		}

	}

	public static void setReceiver(Receiver receiver) {
		SocketUtil.receiver = receiver;
	}

	public static Socket getSocket() {
		return socket;
	}

	public static void setSocket(Socket socket) {
		SocketUtil.socket = socket;
		System.out.println("Client link success");
		receiver.StartThread();
		color=Color.white;

	}

	public static void SendMessage(Object message) {
		try {
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(message);
			oos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Color getColor() {
		return color;
	}

	public static User getUser() {
		return user;
	}

}
